//
//  YXYNextReplyTableViewCell.m
//  Topit
//
//  Created by dllo on 15/12/5.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "YXYNextReplyTableViewCell.h"
#import "UIImageView+WebCache.h"
@implementation YXYNextReplyTableViewCell

- (void)dealloc {
    [_idImage release];
    [_idName release];
    [_ts release];
    [_cont release];
    [_images release];
    [_height release];
    [_replyModel release];
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.idImage = [[UIImageView alloc] initWithFrame:CGRectZero];
        _idImage.layer.cornerRadius = 15;
        _idImage.layer.masksToBounds = YES;
        [self addSubview:_idImage];
        [_idImage release];
        
        self.idName = [[UILabel alloc] initWithFrame:CGRectZero];
        _idName.font = [UIFont systemFontOfSize:11];
        [self addSubview:_idName];
        [_idName release];
        
        self.ts = [[UILabel alloc] initWithFrame:CGRectZero];
        _ts.font = [UIFont systemFontOfSize:8];
        _ts.textColor = [UIColor grayColor];
        [self addSubview:_ts];
        [_ts release];
        
        self.cont = [[UILabel alloc] initWithFrame:CGRectZero];
        _cont.numberOfLines = 0;
        _cont.font = [UIFont systemFontOfSize:10];
        [self addSubview:_cont];
        [_cont release];
        
        self.images = [[UIImageView alloc] initWithFrame:CGRectZero];
        _images.contentMode = UIViewContentModeScaleAspectFill;
        _images.clipsToBounds = YES;
        [self addSubview:_images];
        [_images release];
        
        
    }
    return self;
}

-(void)setReplyModel:(YXYReplyModel *)replyModel {
    if (_replyModel != replyModel) {
        [_replyModel release];
        _replyModel = [replyModel retain];
    }
    if (replyModel.icon.count != 0) {
        _height = _replyModel.icon[@"height"];
    }else {
        _height = @"425";
    }
  
 
    if ([replyModel.icon isKindOfClass:[NSDictionary class]]) {
        [_idImage sd_setImageWithURL:[NSURL URLWithString:replyModel.user[@"icon"][@"url"]]];
        _idName.text = replyModel.user[@"name"];
        _ts.text = replyModel.ts;
//        NSLog(@"%@", replyModel.icon[@"url"]);
        [_images sd_setImageWithURL:[NSURL URLWithString:replyModel.icon[@"url"]]];
        _cont.text = replyModel.cont;
    }

}

-(void)layoutSubviews {
    [super layoutSubviews];
    _idImage.frame = CGRectMake(5, 5, 30, 30);
    CGFloat kOriginX = _idImage.frame.origin.x + _idImage.frame.size.width + 10;
    _idName.frame = CGRectMake(kOriginX, _idImage.frame.origin.y + 3, self.frame.size.width, 10);
    _ts.frame = CGRectMake(kOriginX, _idName.frame.origin.y + 15, 40, 10);
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:10]};
    CGRect rect = [_cont.text boundingRectWithSize:CGSizeMake(self.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    _cont.frame = CGRectMake(5, _idImage.frame.size.height + 10, self.frame.size.width - 5, rect.size.height);
    CGFloat kHeight = _height.floatValue / 2;
    _images.frame = CGRectMake(2, _cont.frame.size.height + _cont.frame.origin.y, self.frame.size.width - 4, kHeight);
}

- (CGFloat)getReplyHeigth {
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:10]};
    CGRect rect = [_cont.text boundingRectWithSize:CGSizeMake(self.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height + _images.frame.size.height + 50;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
